package demo.um.controller;

import java.util.Map;
import java.util.logging.Logger;

import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@RequestScoped
public class ErrorHandler {

	@Inject
	private Logger log;

	public String getErrorMessage() {
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();

		StringBuilder errorMessage = new StringBuilder();

		Throwable throwable = (Throwable) requestMap.get("javax.servlet.error.exception");
		if (throwable != null) {
			errorMessage.append(throwable.getMessage()).append("\n");
			for (StackTraceElement element : throwable.getStackTrace()) {
				errorMessage.append(element).append("\n");
			}
		}

		return errorMessage.toString();
	}

}
